package liangtWorkSpace.threadDemo.thread;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author liangt
 * @ClassName: ConCurrentAble
 * @title: ConCurrentAble
 * @projectName liangtWorkSpace.threadDemo.thread
 * @description: 使用并发包Concurrent实现并发
 * @date 2020/11/3 11:58
 */
public class ConCurrentAble {
    //调度器，创建三个线程池
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executorService =Executors.newFixedThreadPool(3);
        Runner3 luxiang = new Runner3();
        luxiang.setName("刘翔");

        //将刘翔加载进线程池
        Future<Integer> future = executorService.submit(luxiang);
        Thread.sleep(5);
        executorService.shutdown();
        System.out.println(future.get());
    }
}


class Runner3 implements Callable<Integer>{
    private String name;

    private String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Integer call() throws Exception {
        Integer speed = new Random().nextInt(100);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            System.out.println(this.getName()+ "跑了"+speed*i+"米（"+speed+"米/秒）");
        }
        return speed;
    }
}